package com.woniu.test;

import org.flowable.engine.HistoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zcy
 * @Create: 2025/11/5 9:28
 */
@SpringBootTest
public class BootFlowApplication {
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Resource
    private HistoryService historyService;

    @Test
    public void test1(){
        //员工请假
        Map<String, Object> map = new HashMap<>();
        map.put("doctorUser","王五");
        map.put("day",5);
        //通过流程ID启动流程，引擎会推动流程运转，并返回当前流程实例对象
        ProcessInstance doctorLeave = runtimeService.startProcessInstanceByKey("DoctorLeave", map);
        //从流程实例中获取任务
        Task task = taskService.createTaskQuery().processInstanceId(doctorLeave.getId()).singleResult();
        //执行任务
        taskService.complete(task.getId());

        //主任审批

        List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("director").list();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("outcome","通过");
        for (Task task1 : tasks) {
            taskService.complete(task1.getId(),map1);
        }
        //院长审批
        List<Task> tasks1 = taskService.createTaskQuery().taskCandidateGroup("dean").list();
        Map<String, Object> map2 = new HashMap<>();
        map2.put("outcome","通过");
        for (Task task1 : tasks1) {
            taskService.complete(task1.getId(),map2);
        }
        List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
                .processInstanceId(doctorLeave.getId())
                .orderByHistoricActivityInstanceEndTime().asc()
                .list();
        for (HistoricActivityInstance historicActivityInstance : list) {
            System.out.printf("活动名称:%s,开始时间:%s结束时间:%s",historicActivityInstance.getActivityName(),historicActivityInstance.getStartTime(),historicActivityInstance.getEndTime());
        }


    }
}
